﻿#include "alarmlight.h"

#include "systemconfiguration.h"
#include "component_log.h"
#include "componentmanager.h"
#include "../robot/robot.h"

AlarmLight::AlarmLight(QObject *parent)
	: ComponentBase(parent)
{
	m_name = "报警灯组件";
	m_type = Component::component_alarmLight;

}

AlarmLight::~AlarmLight()
{
}

// 组件初始化处理函数
int AlarmLight::ComponentInit()
{
	auto cfg = syscfg->GetAlarmLightSetting();
	if (!cfg.enable) {
		log_error("报警灯未使能，无需创建报警灯组件");
		return COMMON_ERROR;
	}

	log_trace("报警灯配置：红灯：{}，黄灯：{}， 绿灯：{}", cfg.red, cfg.yellow, cfg.green);
	if (cfg.red <= 0 || cfg.red > 32 || cfg.yellow <=0 ||cfg.yellow >32||
		cfg.green <= 0 || cfg.green >32) {
		SetErrmsg("报警灯DI配置错误", false);
		return SERIOUS_ERROR;
	}
	m_red = cfg.red;
	m_green = cfg.green;
	m_yellow = cfg.yellow;

	m_rob = dynamic_cast<Robot*>(component->GetComponent(Component::component_robot));
	if (m_rob == nullptr) {
		SetErrmsg("报警灯组件初始化失败，无法加载机械手组件", false);
		return SERIOUS_ERROR;
	}

	log_error("报警灯组件加载成功");
	return 0;
}
/*自检接口*/
bool AlarmLight::ComponentCheckSelfState()
{
	return true;
}
/*复位接口*/
bool AlarmLight::ComponentSelfReset()
{
	return true;
}

void AlarmLight::ResponseSystemState(int state)
{
	if (state == 1) {
		log_trace("yellow light:");
		SetYellowLight();
	} else if(state == 2){
		log_trace("green light:");
		SetGreenLight();
	} else {
		log_trace("red light:");
		SetRedLight();
	}
}

void AlarmLight::SetRedLight()
{
	m_rob->SetDO(m_red, 1);
	m_rob->SetDO(m_green, 0);
	m_rob->SetDO(m_yellow, 0);
}
void AlarmLight::SetYellowLight()
{
	m_rob->SetDO(m_red, 0);
	m_rob->SetDO(m_green, 0);
	m_rob->SetDO(m_yellow, 1);
}
void AlarmLight::SetGreenLight()
{
	m_rob->SetDO(m_red, 0);
	m_rob->SetDO(m_green, 1);
	m_rob->SetDO(m_yellow, 0);
}
